package com.weiyi.mashangzhu.Landlord.Activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.Landlord.Adapter.DiscountAdapter;
import com.weiyi.mashangzhu.Landlord.Bean.DiscountBean;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetBedActivity extends BaseActivity {

    @BindView(R.id.lv)
    ListView lv;
    List<DiscountBean> list = new ArrayList<>();
    DiscountAdapter discountAdapter=null;
    String bed_type,bed_l,bed_w,bed_count,info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_bed);
        ButterKnife.bind(this);
        Intent intent=getIntent();
        info=intent.getStringExtra("info");
        list.add(new DiscountBean("床型", ""));
        list.add(new DiscountBean("长度", ""));
        list.add(new DiscountBean("宽度", ""));
        list.add(new DiscountBean("数量", ""));
        discountAdapter=new DiscountAdapter(list, this);
        lv.setAdapter(discountAdapter);

        if (info.equals("post")){
            if (MyApp.base_bed_type!=null) {
                String s="";
                if (MyApp.base_bed_type.equals("单")){
                    s="单人床";
                }else if (MyApp.base_bed_type.equals("双")){
                    s="双人床";
                }

                list.get(0).setCount(s);
                discountAdapter.notifyDataSetChanged();
            }
            if (MyApp.base_bed_l!=null) {
                list.get(1).setCount(MyApp.base_bed_l+"米");
                discountAdapter.notifyDataSetChanged();
            }
            if (MyApp.base_bed_w!=null) {
                list.get(2).setCount(MyApp.base_bed_w+"米");
                discountAdapter.notifyDataSetChanged();
            }
            if (MyApp.base_bed_count!=null) {
                list.get(3).setCount(MyApp.base_bed_count+"张");
                discountAdapter.notifyDataSetChanged();
            }
        }else {
            bed_type=intent.getStringExtra("bed_type");
            bed_l=intent.getStringExtra("bed_l");
            bed_w=intent.getStringExtra("bed_w");
            bed_count=intent.getStringExtra("bed_count");
            list.get(0).setCount(MyApp.re_bedtype+"人床");
            list.get(1).setCount(MyApp.re_bedl+"米");
            list.get(2).setCount(MyApp.re_bedw+"米");
            list.get(3).setCount(MyApp.re_bedcount+"张");
        }


        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        showPopupWindow_wei();
                        break;
                    case 1:
                        price("长度");
                        break;
                    case 2:
                        price("宽度");
                        break;
                    case 3:
                        price("数量");
                        break;
                }
            }
        });

    }
    private void price(final String s) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = View.inflate(this, R.layout.dialog_price_set, null);
        final TextView baocun = (TextView) view.findViewById(R.id.baocun);
        final EditText price = (EditText) view.findViewById(R.id.price);

        if (s.equals("数量")){
            price.setInputType(InputType.TYPE_CLASS_NUMBER);
        } else {
            price.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        }

        builder.setView(view);
        final AlertDialog dialog = builder.show();
        baocun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (s.equals("长度")){
                    list.get(1).setCount(price.getText()+"米");
                    discountAdapter.notifyDataSetChanged();
                    MyApp.base_bed_l=price.getText()+"";
                }else  if (s.equals("宽度")){
                    list.get(2).setCount(price.getText()+"米");
                    discountAdapter.notifyDataSetChanged();
                    MyApp.base_bed_w=price.getText()+"";
                }else  if (s.equals("数量")){
                    price.setInputType(InputType.TYPE_CLASS_NUMBER);
                    list.get(3).setCount(price.getText()+"张");
                    discountAdapter.notifyDataSetChanged();
                    MyApp.base_bed_count=price.getText()+"";
                }
            }
        });
    }
    public void showPopupWindow_wei() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_win_bed, null);
        final PopupWindow popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        TextView pop_friend_wx = (TextView) contentView.findViewById(R.id.pop_friend_wx);
        TextView pop_friend_wxc = (TextView) contentView.findViewById(R.id.pop_friend_wxc);
        TextView pop_off = (TextView) contentView.findViewById(R.id.pop_off);
        pop_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        pop_friend_wx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                list.get(0).setCount("单人床");
                discountAdapter.notifyDataSetChanged();
                MyApp.base_bed_type="单";

            }
        });
        pop_friend_wxc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                list.get(0).setCount("双人床");
                discountAdapter.notifyDataSetChanged();
                MyApp.base_bed_type="双";
            }
        });
        popupWindow.setTouchable(true);
        popupWindow.setAnimationStyle(R.style.popup_window_bottom_animation);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x55000000));
        // 设置好参数之后再show
        if (popupWindow.isShowing()) {

        } else {
            popupWindow.showAsDropDown(contentView);
        }

    }
    @OnClick({R.id.back, R.id.baocun})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.baocun:
                Intent intent = new Intent();
                if (info.equals("post")){

                    if (MyApp.base_bed_type!=null && MyApp.base_bed_l!=null&&MyApp.base_bed_w!=null && MyApp.base_bed_count!=null) {
                        intent.putExtra("base_bed_type", MyApp.base_bed_type);
                        intent.putExtra("base_bed_l", MyApp.base_bed_l);
                        intent.putExtra("base_bed_w", MyApp.base_bed_w);
                        intent.putExtra("base_bed_count", MyApp.base_bed_count);
                        setResult(0x002, intent);
                        finish();
                    }else {
                        T.showShort(SetBedActivity.this,"请完善床铺信息");
                    }
                }else {
                    TextView bedtype= (TextView) lv.getChildAt(0).findViewById(R.id.count);
                    TextView bedl= (TextView) lv.getChildAt(1).findViewById(R.id.count);
                    TextView bedw= (TextView) lv.getChildAt(2).findViewById(R.id.count);
                    TextView bedcount= (TextView) lv.getChildAt(3).findViewById(R.id.count);
                    MyApp.re_bedtype=(bedtype.getText()+"").substring(0,(bedtype.getText()+"").indexOf("人"));
                    MyApp.re_bedl=(bedl.getText()+"").substring(0,(bedl.getText()+"").indexOf("米"));
                    MyApp.re_bedw=(bedw.getText()+"").substring(0,(bedw.getText()+"").indexOf("米"));
                    MyApp.re_bedcount=(bedcount.getText()+"").substring(0,(bedcount.getText()+"").indexOf("张"));
//                    intent.putExtra("base_bed_type", (bedtype.getText()+"").substring(0,(bedtype.getText()+"").indexOf("人")));
//                    intent.putExtra("base_bed_l", (bedl.getText()+"").substring(0,(bedl.getText()+"").indexOf("米")));
//                    intent.putExtra("base_bed_w", (bedw.getText()+"").substring(0,(bedw.getText()+"").indexOf("米")));
//                    intent.putExtra("base_bed_count",(bedcount.getText()+"").substring(0,(bedcount.getText()+"").indexOf("张")));
//                    setResult(0x002, intent);
                    finish();

                }

                break;
        }
    }
}
